package tang.Controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tang.domain.Goods;
import tang.service.GoodsService;

@RestController
@RequestMapping("/Goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /*
    * 降级:
    *   1:出现异常
    *   2.服务调用超时
    * */

    @HystrixCommand(fallbackMethod = "findOne_fallback") //指定降级后调用的方法
    @GetMapping("/findOne/{id}")
    public Goods findOne(@PathVariable("id") int id){


       if (id==1){
          // 1.出现异常会触发降级
            int i =3/0;
       }
        //2.超时调用超时
       /* try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
*/
        Goods goods = goodsService.findOne(id);
        return goods;
    }

    //定义降级方法: 1.返回值和原方法一致 , 2.方法参数要和原方法一致
    public Goods findOne_fallback(int id){
       Goods goods =new Goods();
       goods.setTitle("降级了~~~");
        return goods;
    }
}
